package com.yj.core.helper;

import com.yj.core.constants.AuthConstants;
import com.yj.core.constants.GlobalConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 认证工具类
 *
 * @author cyj
 * @date 19-03-13
 */
public class RequestHelper {

    /**
     * 获取当前请求的Request对象
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        } else {
            return requestAttributes.getRequest();
        }
    }

    /**
     * 获取当前请求的Response对象
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        } else {
            return requestAttributes.getResponse();
        }
    }


    /**
     * 获取请求API-KEY
     *
     * @param request
     * @return
     */
    public static String getApiKey(HttpServletRequest request) {
        String apikey = request.getHeader(AuthConstants.HTTP_HEADER_APIKEY);
        if (StringUtils.isNotBlank(apikey)) {
            return apikey;
        } else {
            return request.getParameter(AuthConstants.HTTP_HEADER_APIKEY);
        }
    }

    /**
     * 获取请求userToken
     *
     * @param request
     * @return
     */
    public static String getUserToken(HttpServletRequest request) {
        String userToken = request.getHeader(AuthConstants.HTTP_HEADER_TOKEN);
        if (StringUtils.isNotBlank(userToken)) {
            return userToken;
        } else {
            return request.getParameter(AuthConstants.HTTP_HEADER_TOKEN);
        }
    }

    /**
     * 获取请求 app版本
     *
     * @return
     */
    public static String getAppVersion() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attrs.getRequest();
        String appversion = request.getHeader(AuthConstants.HTTP_HEADER_APP_VERSION);
        if (StringUtils.isBlank(appversion)) {
            appversion = request.getParameter(AuthConstants.HTTP_HEADER_APP_VERSION);
        }
        return appversion;
    }

    /**
     * 获取登录类型(1 pc 2 app)
     *
     * @return
     */
    public static Integer getLoginType() {
        HttpServletRequest request = getRequest();
        String appversion = request.getHeader(AuthConstants.HTTP_HEADER_LOGIN_TYPE);
        if (StringUtils.isBlank(appversion)) {
            appversion = request.getParameter(AuthConstants.HTTP_HEADER_LOGIN_TYPE);
        }
        if (StringUtils.isBlank(appversion)) {
            return null;
        }
        return Integer.valueOf(appversion);
    }

    /**
     * 获取微服务请求来源
     *
     * @param request
     * @return
     */
    public static String getRequestSource(HttpServletRequest request) {
        String requestSource = request.getHeader(GlobalConstants.REQUEST_GW);
        if (StringUtils.isNotBlank(requestSource)) {
            return requestSource;
        } else {
            return request.getParameter(GlobalConstants.REQUEST_GW);
        }
    }

    /**
     * 获取微服务请求来源
     *
     * @param request
     * @return
     */
    public static String getRequestNo(HttpServletRequest request) {
        String requestSource = request.getHeader(GlobalConstants.REQUEST_NO);
        if (StringUtils.isNotBlank(requestSource)) {
            return requestSource;
        } else {
            return request.getParameter(GlobalConstants.REQUEST_NO);
        }
    }
}